/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vmwapplication.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import vmwapplication.data.Test;
import vmwapplication.exception.VMWAppException;

/**
 *
 * @author marek
 */
public class Parser {
    
    static public List<Test> parse(String absPath) throws VMWAppException{
        
            List<Test> tests = new ArrayList<Test>();
            File inFile = new File(absPath);
            Scanner scanner = null;
            try {
                    
                    scanner = new Scanner(inFile);
            } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    throw new VMWAppException("Chyba nacitani souboru", e);
            }
            scanner.useDelimiter(">");
            String line;
            
            line = scanner.next(); // it returns null or empty string because of format of file
            while(scanner.hasNext()){
                line = scanner.next();
                String dnastring = line.split("]")[1].replace("\n", "");
                String name = line.split("]")[0].split("\\[")[1];
                Test test = new Test();
                test.setName(name);
                dnastring = dnastring.replaceAll("[\u0000-\u001f]", "");
                test.setDnastring(dnastring);
                tests.add(test);
            }        
            scanner.close();
        return tests;
    }
    
    static public String parseDNAsearchStringFile(String absPath) throws VMWAppException{
        File inFile = new File(absPath);
        BufferedReader br = null;
        try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile)));
        } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new VMWAppException("Chyba nacitani dna souboru", e);
        }

        String line;
        try {// dna retezec na jednom radku
            line = br.readLine();
            br.close();     
        } catch (IOException ex) {
            try {
                br.close();
            } catch (IOException ex1) {
                Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex1);
            }
            throw new VMWAppException("Chyba parsovani dna souboru", ex);
            
        }
        return line.replaceAll("[\u0000-\u001f]", "");
    }
    
}
